package com.hama.zea.tbUser.service;

import com.hama.iotrust.common.ServiceException;
import com.hama.iotrust.common.mybatis.criteria.Criteria;
import com.hama.iotrust.common.mybatis.result.Pager;
import com.hama.zea.tbUser.entity.TbUser;
import com.hama.zea.tbUser.mapper.TbUserMapper;
import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务实现层 - 表：tb_user
 * @since 2016-07-06 12:01:07
 */
@Service("tbUserService")
public class TbUserService implements Serializable {
	private static final long serialVersionUID = 1L;

	@Resource
	private TbUserMapper tbUserMapper;

	public void insert(TbUser entity) throws ServiceException {
		try {
			tbUserMapper.insert(entity);
		} catch (Exception e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	public void delete(TbUser entity) throws ServiceException {
		try {
			tbUserMapper.deleteByPrimaryKey(entity.getId());
		} catch (Exception e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Transactional
	public void batchDelete(List<String> ids) throws ServiceException {
		try {
			for (String id : ids) {
				tbUserMapper.deleteByPrimaryKey(id);
			}
		} catch (Exception e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	public void update(TbUser entity) throws ServiceException {
		try {
			tbUserMapper.updateByPrimaryKey(entity);
		} catch (Exception e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	public void updateSelective(TbUser entity) throws ServiceException {
		try {
			tbUserMapper.updateByPrimaryKeySelective(entity);
		} catch (Exception e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	public TbUser findByPrimaryKey(String id) throws ServiceException {
		try {
			return tbUserMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	public Pager<TbUser> findPage(Criteria<TbUser> criteria) throws ServiceException {
		try {
			if (criteria == null) {
				 criteria = Criteria.create(TbUser.class);
			}
			criteria.pagination(true);
			List<TbUser> list = tbUserMapper.selectByCriteria(criteria);
			return new Pager<TbUser>(criteria, list);
		} catch (Exception e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	public List<TbUser> findAll(Criteria<TbUser> criteria) throws ServiceException {
		try {
			if (criteria == null) {
				 criteria = Criteria.create(TbUser.class);
			}
			criteria.pagination(false);
			return tbUserMapper.selectByCriteria(criteria);
		} catch (Exception e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
}